package com.example.com.daervideo;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import java.io.File;
import java.util.HashMap;

/**
 * Created by i7-1 on 2018/12/10.
 */

public class TestActivity extends AppCompatActivity {

    private ImageView imageView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        initView();
    }

    private void initView() {
        imageView = findViewById(R.id.img);
        findViewById(R.id.getimg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  getVideoThumbnail("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        final Bitmap bitmap = getBitmap(TestActivity.this, "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4", false);
                        ///final Bitmap bitmap = getBitmap(TestActivity.this, "http://221.228.226.5/14/z/w/y/y/zwyyobhyqvmwslabxyoaixvyubmekc/sh.yinyuetai.com/4599015ED06F94848EBF877EAAE13886.mp4", false);
                        //final Bitmap bitmap = getBitmap(TestActivity.this,
//                                "http://221.228.226.5/14/z/w/y/y/zwyyobhyqvmwslabxyoaixvyubmekc/sh.yinyuetai.com/4599015ED06F94848EBF877EAAE13886.mp4",
//                                false);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //将bitmap对象转换成drawable对象
                                Drawable drawable=new BitmapDrawable(bitmap);
                                imageView.setImageDrawable(drawable);
                            }
                        });
                    }
                }).start();
            }
        });
    }


    public Bitmap getVideoThumbnail(String videoUrl) {

//            Bitmap generateBitmap = null;
//            FFmpegMediaMetadataRetriever fmmr = new FFmpegMediaMetadataRetriever();
//            try {
//                fmmr.setDataSource(videoUrl);
//                generateBitmap = fmmr.getFrameAtTime();
//
//                if (generateBitmap != null) {
//                    Bitmap b2 = fmmr.getFrameAtTime();
//                    if (b2 != null) {
//                        // 获取到了Bitmap之后用android自带的ThumbnailUtils获取自定义大小的略缩图
//                        generateBitmap = ThumbnailUtils.extractThumbnail(b2, 640,
//                                640, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
//                    }
//                }
//            } catch (IllegalArgumentException ex) {
//                ex.printStackTrace();
//            } finally {
//                fmmr.release();
//            }
//            if (generateBitmap != null) {
//                return generateBitmap;
//            }
        return null;
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static Bitmap getBitmap(Context context, String url, boolean isSD) {
        Bitmap bitmap = null;
        //MediaMetadataRetriever 是android中定义好的一个类，提供了统一
        //的接口，用于从输入的媒体文件中取得帧和元数据；
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            if (isSD) {
                //（）根据文件路径获取缩略图
                retriever.setDataSource(context, Uri.fromFile(new File(url)));
            } else {
                //根据网络路径获取缩略图
                retriever.setDataSource(url, new HashMap<String, String>());
            }
            //获得第一帧图片
            bitmap = retriever.getFrameAtTime(0, MediaMetadataRetriever.OPTION_NEXT_SYNC);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            retriever.release();
        }

        Log.e("Image", bitmap + "");
        return bitmap;
    }
}
